package semantic.rules.walk

import syntax.{CGrammar, CASTNode}
import scala.collection.mutable.Buffer
import CGrammar._
import utils.data.CTree
import semantic.COperations._

object CWalkRuleExpression extends CWalkRule
{
    override val categories = CExpression :: Nil

    override def post(node:CASTNode)(implicit tree:CTree[CASTNode], code:Buffer[String]) =
        if (node().parent.is(_.category == CFlowUnit) &&
           !(node().parent.parent.is(_.category == CIfElse) ||
           (node().parent.parent.parent.is(_.category == CIfElse) && node().parent.parent.is(_.category == CFlow))) &&
           !(node().parent.parent.is(_.category == CFunctionDefinition) ||
           (node().parent.parent.parent.is(_.category == CFunctionDefinition) && node().parent.parent.is(_.category == CFlow)))) code += POP

//        if (node().parent.is(_.category == CArithmeticExpression) ||
//            node().parent.is(_.category == CWhile) ||
//            node().parent.is(_.category == CAssign) ||
//            node().parent.is(_.category == CFunctionExpression)  ||
//            node().parent.is(_.category == CFunctionCall)  ||
//            node().parent.parent.parent.is(_.category == CFunctionDefinition) ||
//            node().parent.parent.is(_.category == CFunctionDefinition) unary_!) code += POP
}
